package com.offcn;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class TextExcel {
    @Test
    public void testwrite07() throws IOException {
        //创建新的  Excel工作薄
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 如要新建一名为"会员登录统计"的工作表，其语句为：
        Sheet sheet = workbook.createSheet("工资表");
        // 创建行（row 0）
        Row row0 = sheet.createRow(0);
        //创建列设置值
        Cell row0cell0 = row0.createCell(0);
        row0cell0.setCellValue("员工名称");
        Cell row0Cell1 = row0.createCell(1);
        row0Cell1.setCellValue("员工薪资");

        // 创建行（row 1）
        Row row1 = sheet.createRow(1);
        //创建列设置值
        Cell row1cell1 = row1.createCell(0);
        row1cell1.setCellValue("李飞");
        Cell row1Cell1 = row1.createCell(1);
        row1Cell1.setCellValue("250");
        try {
            //新建流
            FileOutputStream fileOutputStream = new FileOutputStream("F:/biaodan/test07.xlsx");
            //写入
            workbook.write(fileOutputStream);
            fileOutputStream.close();
            System.out.println("文件生成成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件生成失败");

        }


    }
    @Test
    public void testRead07() throws Exception{
        InputStream is = new FileInputStream("F:/biaodan/test07.xlsx");
        Workbook workbook = new XSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);
        // 读取第一行第一列
        Row row = sheet.getRow(1);
        Cell cell = row.getCell(1);
        // 输出单元内容
        System.out.println(row.getCell(0)+"----"+cell.getStringCellValue());
        // 操作结束，关闭文件
        is.close();
    }

}
